/*
Copyright (c) 2008 SmashedApples 

Contributors.  See: http://smashedapples.googlecode.com/

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package smashedapples.amazon.simpledb.data
{
	import mx.core.Application;
	
	import smashedapples.core.Apples;
	
	public class SimpleDBDataUtils
	{
		
		
		public static function get offset():Number
		{
			return (Application.application as Apples).awsNumberOffset
		}
		
		public static function encodeValue(type:String,value:*=null):*
		{
			
			if(value == null)
				return "";
			
			var val : * = "";

			switch( type )
			{
				
				// dates need to be stored as empty strings or the GMT 
				case "Date":
					
					if( !value == "")
					{
						val = (value as Date).getTime().toString();
					}
						
						
				break;
				
				
				//Numbers are offset 
				case "Number":
			
	
					if(isNaN(Number(value)))
					{
						val = Number(SimpleDBDataUtils.offset).toString();
					}
					else
					{
						val = Number(SimpleDBDataUtils.offset + Number(value)).toString();	
					}
				
				break;
				
				
				//itemID and strings are OK to send as is.
				case "itemID":
				case "String":
					val = value;
				break;
			}
			
			return val;
		}
		
		
		public static function decodeValue(type:String,value:*=null):*
		{
			var val : * = "";

			switch( type )
			{
				
				// dates are returned as GMT numbers and need to be converted back into Dates
				case "Date":
					if( !value == "")
					{
						val = new Date( Number(value) );
					}	
				break;
				
				
				//The Number offset needs to be subtracted 
				case "Number":
					
					if(isNaN(Number(value)))
					{
						val = "";
					}
					else
					{
						val = Number( Number(value) - SimpleDBDataUtils.offset);	
					}
	
				break;
				
				
				//itemID and strings are OK to send as is.
				case "itemID":
				case "String":
					val = value;
				break;
			}
			
			return val;
		}
		
	}
}